<template>
  <div class="login-container">
    <!-- 弹出层 -->
    <el-dialog :title="formTitle" :visible.sync="dialogFormVisible">
      <el-steps :active="active" finish-status="success">
        <el-step title="注册" description="注册已完成">已完成</el-step>
        <el-step title="审核中" description="。。。。。">已完成</el-step>
        <el-step title="审核结果" description="未通过"></el-step>
      </el-steps>
    </el-dialog>

    <el-form
      ref="loginForm"
      :rules="rules"
      :model="loginForm"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">欢迎登录</h3>
      </div>
      <!-- 用户名 -->
      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          name="username"
          v-model="loginForm.username"
          placeholder="请输入姓名"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <!-- 请输入密码 -->
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          ref="password"
          v-model="loginForm.password"
          placeholder="请输入密码"
          :type="passwordType"
          name="password"
          tabindex="2"
          auto-complete="on"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon
            :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
          />
        </span>
      </el-form-item>

      <!-- 登录 -->
      <el-button
        type="primary"
        style="width:48%;margin-bottom:30px;"
        @click="handleLogin"
        >登录</el-button
      >

      <!-- 注册 -->
      <el-button
        type="primary"
        style="width:48%;margin-bottom:30px;"
        @click="handleReg"
        >注册</el-button
      >
    </el-form>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    // 用户名自定义验证(必须已字母开头，允许5-16字节，允许字母数字下划线)
    let usernameReg = /^[a-zA-Z][a-zA-Z0-9_]{4,15}$/;
    let validateUsername = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("用户名不能为空！！！"));
      }
      if (!usernameReg.test(value)) {
        callback(
          new Error("必须已字母开头，允许5-16字节，允许字母数字下划线！！！")
        );
      } else {
        callback();
      }
    };
    // 密码自定义验证(以字母开头，长度在6~18之间，只能包含字母、数字和下划线)
    let passwordReg = /^[a-zA-Z]\w{5,17}$/;
    let validatePassword = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("密码不能为空！！！"));
      }
      if (!passwordReg.test(value)) {
        callback(
          new Error(
            "必须以字母开头，长度在6~18之间，只能包含字母、数字和下划线！！！"
          )
        );
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        // 用户名
        username: "",
        // 密码
        password: ""
      },
      // 密码是否显示密文或者明文
      passwordType: "password",
      // 校验规则
      rules: {
        // 用户名
        username: [
          { required: true, validator: validateUsername, trigger: "blur" }
        ],
        // 密码
        password: [
          { required: true, validator: validatePassword, trigger: "blur" }
        ]
      },
      active: 1,
      dialogFormVisible: false, //控制增加和修改的表单是否显示
      formLabelWidth: "120px", //表单的label的宽度
      formTitle: "" //表单的标题
    };
  },
  methods: {
    // 点击切换密码是否显示明文或者密文
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    // 注册
    handleReg() {
      this.$router.push({ path: "reg" });
    },
    // 登录
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.$store
            .dispatch("user/login", this.loginForm)
            .then(res => {
              //状态(申请中  /已通过  /未通过  )
              if (res.status == "申请中") {
                this.dialogFormVisible = true;
                // 申请中 不能登录后台，等审核通过后可登录
                this.active = 2;
              }
              if (res.status == "已通过") {
                // 已通过审核,可直接登录
                location.href = "http://localhost:9528/";
              }
              if (res.status == "未通过") {
                this.dialogFormVisible = true;
                this.active = 3;
              }
            })
            .catch(() => {});
        }
      });
    }
  }
};
</script>

<style lang="scss">
$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
